// 
//
//	File: AbstractMaterial.cpp
//
// Implements the AbstractMaterial class.

#include "AbstractMaterial.h"
#include <cassert>



// statics
AbstractMaterial::MaterialList AbstractMaterial::dynamicMaterials_(INIT_DYNAMICMATERIAL_ARRAY_NUM);



AbstractMaterial::~AbstractMaterial()
{
	SetDynamic( false );
}



void AbstractMaterial::PreRender( int pass )
{
	assert( pass < passes_.Count() );
	passes_[pass]->PreRender();
}



void AbstractMaterial::PostRender( int pass )
{
	assert( pass < passes_.Count() );
	passes_[pass]->PostRender();
}



void AbstractMaterial::SetDynamic( bool dynamic )
{
	if( dynamic != dynamic_ )
	{
		dynamic_ = dynamic;
		if( dynamic_ )
		{
			if( !dynamicMaterials_.Push(this) )
			{
				dynamicMaterials_.Resize( INIT_DYNAMICMATERIAL_ARRAY_NUM + dynamicMaterials_.Size() );
				dynamicMaterials_.Push(this);
			}
			
		}
		else
		{
			for(int i=0; i<dynamicMaterials_.Count(); i++)
			{
				if( dynamicMaterials_[i] == this )
				{
					dynamicMaterials_[i] = dynamicMaterials_.Top();
					dynamicMaterials_.Pop();
					return;
				}
			}			
		}
	}
}



void AbstractMaterial::UpdateDynamicMaterials( float elapsedTime )
{
	for(int i=0; i<dynamicMaterials_.Count(); i++)
	{
		dynamicMaterials_[i]->Update(elapsedTime);
	}	
}
